﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.DAL.LocalDataAccess/ModuleShieldAccess
* 类       名: ModuleShieldAccess
* 功      能:  TD
* 时      间:  2024-05-18 14:02:43
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using log4net.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using WaferSelectV3.Common;
using WaferSelectV3.DAL.DataEntity;
using WaferSelectV3.Extensions;
using WaferSelectV3.Services;

namespace WaferSelectV3.DAL.LocalDataAccess
{
    public class ModuleShieldAccess
    {
        /// <summary>
        /// 获取所有的屏蔽模块的数据
        /// </summary>
        /// <returns></returns>
        public static DataResult<List<ModuleShieldEntity>> GetTotalShieldModules()
        {
            DataResult<List<ModuleShieldEntity>> result = new DataResult<List<ModuleShieldEntity>>();
            try
            {
                using (var dataContext = new DataContext())
                {
                    result.Data = dataContext.ModuleShields.ToList();
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        public static DataResult AddDataMany(List<ModuleShieldEntity> initData)
        {
            DataResult result = new DataResult();
            try
            {
                foreach (var entity in initData)
                {
                    var singleResult = AddDataSingle(entity);
                    if (!singleResult.State)
                    {
                        return singleResult;
                    }
                }
                result.State = true;
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }


        public static DataResult AddDataSingle(ModuleShieldEntity entity)
        {
            DataResult result = new DataResult();
            try
            {
                using (var dataContext = new DataContext())
                {
                    dataContext.ModuleShields.Add(entity);
                    int rowAffected = dataContext.SaveChanges();
                    if (rowAffected > 0)
                    {
                        result.State = true;
                    }
                    else
                    {
                        result.Message = ConstantProvider.DatabaseAddError;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }


        public static DataResult UpdateDataMany(List<ModuleShieldEntity> moduleShields)
        {
            DataResult result = new DataResult();
            try
            {
                using (var dataContext = new DataContext())
                {
                    foreach (var entity in moduleShields)
                    {
                        var oldEntity = dataContext.ModuleShields.FirstOrDefault(old => old.Id == entity.Id);
                        if (oldEntity == null)
                        {
                            result.Message = $"要更新的数据不存在,更新的模块名称: {entity.ModuleName}";
                            return result;
                        }
                        else
                        {
                            dataContext.Entry(oldEntity).CurrentValues.SetValues(entity);
                            int affectdRows = dataContext.SaveChanges();
                            if (affectdRows < 0)
                            {
                                result.Message = ConstantProvider.DatabaseUpdateError;
                                return result;
                            }
                        }
                    }
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }
    }
}
